/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.exporters;

import java.io.IOException;

import org.bugstat.common.bugs.Component;
import org.bugstat.common.bugs.Platform;
import org.bugstat.common.bugs.Priority;
import org.bugstat.common.bugs.Product;
import org.bugstat.common.bugs.Resolution;
import org.bugstat.common.bugs.Severity;
import org.bugstat.common.bugs.Status;
import org.bugstat.common.bugs.User;

/**
 * This class is used to read bug temporal info. See java docs for {@link BugTemporalInfoWriter} for 
 * format of read file. 
 *
 * @author Basil Shikin
 *
 */
public class BugTemporalInfoReader
{
    private final UserReferenceStorage    userReferenceStorage;
    private final ComponentReferenceStorage componentReferenceStorager;
    private final ProductReferenceStorage   productReferenceStorage;
    
    public BugTemporalInfoReader( UserReferenceStorage userReferenceStorage, 
                                  ComponentReferenceStorage componentReferenceStorage,
                                  ProductReferenceStorage productReferenceStorage )
    {
        if ( userReferenceStorage == null )      throw new IllegalArgumentException("Invalid user reference manager specified");
        if ( componentReferenceStorage == null ) throw new IllegalArgumentException("Invalid component reference manager specified");
        if ( productReferenceStorage == null ) throw new IllegalArgumentException("Invalid product reference manager specified");
        
        this.userReferenceStorage       = userReferenceStorage;
        this.componentReferenceStorager = componentReferenceStorage;
        this.productReferenceStorage    = productReferenceStorage;
    }

    
    public User readAssignedTo(int bugID, RecordStorage file ) throws IOException
    {
        return userReferenceStorage.resolveObject( file.readShort( bugID, 0 ) );
    }
    
    public Status readStatus(int bugID, RecordStorage file ) throws IOException
    {
        return Status.fromByte( file.readByte( bugID, 2 ) );
    }
    
    public Resolution readResolution(int bugID, RecordStorage file ) throws IOException
    {
        return Resolution.fromByte( file.readByte( bugID, 3 ) );
    }
    
    public Priority readPriority(int bugID, RecordStorage file) throws IOException
    {
        return Priority.fromByte( file.readByte( bugID, 4 ) );
    }
    
    public Severity readSeverity(int bugID, RecordStorage file) throws IOException
    {
        return Severity.fromByte( file.readByte( bugID, 5 ) );
    }

    public Platform readPlatform(int bugID, RecordStorage file) throws IOException
    {
        return Platform.fromByte( file.readByte( bugID, 6 ) );
    }
    
    public Component readComponent(int bugID, RecordStorage file ) throws IOException
    {
        return componentReferenceStorager.resolveObject( file.readShort( bugID, 7 ) );
    }
    
    public Product readProduct(int bugID, RecordStorage file ) throws IOException
    {
        return productReferenceStorage.resolveObject( file.readShort( bugID, 9 ) );
    }
    
    public short readCommentsNumber(int bugID, RecordStorage file ) throws IOException
    {
        return file.readShort(bugID, 11 );
    }
}
